package com.example.miaosha.controller;

import com.example.miaosha.entity.SeUser;
import com.example.miaosha.entity.base.BaseResponse;
import com.example.miaosha.service.SeUrlService;
import com.example.miaosha.service.SeckillService;
import com.example.miaosha.vo.SeckillPayVo;
import com.example.miaosha.vo.SeckillTestVo;
import com.example.miaosha.vo.SeckillUrlVo;
import com.example.miaosha.vo.SeckillVo;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Objects;

/**
 * 秒杀接口
 *
 * @author 涂涂
 * @DateTime: 2022/1/5 20:50
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Resource
    private SeckillService seckillService;
    @Resource
    private SeUrlService seUrlService;

    @ApiOperation("秒杀商品")
    @PostMapping("/buy")
    public BaseResponse scekill(@RequestBody @Valid SeckillVo vo) {
        return seckillService.seckill(vo);
    }

    @ApiOperation("支付商品")
    @PostMapping("/pay")
    public BaseResponse pay(@RequestBody SeckillPayVo vo) {
        seckillService.payOrder(vo);
        return BaseResponse.SUCCESS;
    }

    @ApiOperation("压力测试支付接口")
    @PostMapping("/testPay")
    public BaseResponse testPay(@RequestBody SeckillTestVo seckillTestVo) {
        return seckillService.payTestOrder(seckillTestVo);
    }

    @ApiOperation("对外秒杀商品")
    @GetMapping("/dynamicBuy/{url}")
    public BaseResponse dynamicSeckillUrl(@PathVariable String url) {
        String actId = seUrlService.findAct(url);
        // 个人认为还是不要做一层转发比较好，省的浪费资源
        if (StringUtils.isNotBlank(actId)) {
            SeckillVo vo = new SeckillVo();
            vo.setActId(actId);
            return seckillService.seckill(vo);
        } else {
            return BaseResponse.error("链接失效或者未开启");
        }
    }

    /**
     * /seckill/dynamicBuy/BRbMjqYFnEfeeUZn2yBjqaea  CD709130408973053953
     * /seckill/dynamicBuy/rqaUNzQn2eQrQvMJnaNz6ZFj  OX709030903464341505
     * @param vo
     * @return
     */
    @ApiOperation("生成对外秒杀接口链接")
    @PostMapping("/getDynamicBuy")
    public BaseResponse getdynamicUrl(@RequestBody SeckillUrlVo vo) {
        String url = seckillService.getUrl(vo);
        return BaseResponse.success(url);
    }


    @GetMapping("/openDynamicUrl/{actId}")
    public BaseResponse openDynamicUrl(@PathVariable String actId){
        return BaseResponse.success(seUrlService.open(actId));
    }

    @ApiOperation("获取动态链接地址")
    @GetMapping("/dynamicUrl/{actId}")
    public BaseResponse get(@PathVariable String actId){
        return BaseResponse.success(seUrlService.findOne(actId));
    }

}
